home *** CD-ROM | disk | FTP | other *** search
- Path: news.compuserve.com!newsmaster
- From: 76623,2065@compuserve.com (Bobby Martin)
- Newsgroups: comp.lang.c++
- Subject: Re: a question about abstract base classes and libraries
- Date: 5 Feb 1996 14:55:13 GMT
- Organization: CompuServe Incorporated
- Message-ID: <4f55oh$mu5@dub-news-svc-4.compuserve.com>
- References: <4es0b1$au7@news.sdd.hp.com>
- Reply-To: 76623,2065@compuserve.com (Bobby Martin)
- NNTP-Posting-Host: dd36-155.compuserve.com
- X-Newsreader: IBM NewsReader/2 v1.03
-
- In <4es0b1$au7@news.sdd.hp.com>, Laura Mansfield <laura> writes:
- >I'm writing a library, lib.a, to be linked to by some client code that
- ..
- >
- >I'd like to hide the private data members of this class, and so have
- >decided to create and abstract base class, and make visible to the client
- >code only the derived class. To better explain, here's an example:
- >
- >In my library code:
- >
- >class Abstract
- >{
- >
- > protected:
- >
- > int a;
- > int b;
- >
- > public:
- >
- > virtual void func( void ) = 0;
- >};
- >
- >In lib.h:
- >
- >class ClassA : public abstract
- >{
- >
- > public:
- >
- > virtual void func( void );
- >};
- >
- >But I'm beginning to realize I can't really do this w/out defining
- >class Abstract in lib.h. I tried to do "class Abstract;" at the beginning
- >of lib.h, but I think this only works for pointers.
- >
- >Can anyone help me w/ what to do here?
- >
- >Laura
-
- I just thought of this while reading your post, so I haven't tried it, but the
- following should work:
-
- make abstract a member variable:
-
- class ClassA
- {
- private:
- ..
- abstract* holdsAllData;
- ..
- }
- and create a new one with 'holdsAllData = new abstract' in each ClassA
- constructor. Then when you need to access ClassA data, use a format such as
-
- holdsAllData->a;
-
- Then you only need to #include abstract in ClassA's implementation file, not
- in the header. A forward declaration will be sufficient there.
-
- PS Don't forget to delete holdsAllData in the destructor : )
-